package day52.basic.service.impl;

import day52.basic.commonenum.ErrorCode;
import day52.basic.constants.Constant;
import day52.basic.domain.dto.SmsRegisteDto;
import day52.basic.exception.CustomException;
import day52.basic.service.IRegisterService;
import day52.basic.util.MD5Utils;
import day52.basic.util.StrUtils;
import day52.user.domain.Logininfo;
import day52.user.domain.User;
import day52.user.mapper.LogininfoMapper;
import day52.user.mapper.UserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class RegisterServiceImpl implements IRegisterService {
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional
    public void registe(SmsRegisteDto dto) {
        //判断用户是否已经注册
        String phone = dto.getPhone();
        User user = userMapper.findByPhone(phone);
        //如果用户已存在
        if(user != null){
            throw new CustomException(ErrorCode.PHONE_REPEAT.getCode());
        }
        //校验两次填写的密码是否一致
        if(!dto.getPassword().equals(dto.getConfirmPassword())){
            throw new CustomException(ErrorCode.PASSWORDS_INCONSISTENT.getCode());
        }
        //拼接短信验证码的key
        String smsKey = dto.getPhone() + Constant.REGISTER_KEY;
        //从redis中查询短信验证码
        String smsCodeValue = (String)redisTemplate.opsForValue().get(smsKey);
        //没有短信验证码则是过期了
        if(smsCodeValue == null){
            throw new CustomException(ErrorCode.SMSCODE_TIME_OUT.getCode());
        }
        //有短信验证码，校验
        String realCode = smsCodeValue.split(":")[0];
        if(!dto.getSmsCode().equals(realCode)){
            throw new CustomException(ErrorCode.SMSCODE_ERROR.getCode());
        }
        //生成随机字符串盐值
        String salt = StrUtils.getComplexRandomString(32);
        //对密码进行MD5加密
        String md5Pwd = MD5Utils.encrypByMd5(dto.getPassword() + salt);
        //存入logininfo表
        Logininfo logininfo = initLogininfo(dto, salt, md5Pwd);
        logininfoMapper.add(logininfo);
        //存入user表
        user = loginInfoToUser(logininfo);
        userMapper.add(user);
    }

    private Logininfo initLogininfo(SmsRegisteDto dto, String salt, String md5Pwd) {
        Logininfo logininfo= new Logininfo();
        logininfo.setUsername(dto.getPhone());
        logininfo.setPhone(dto.getPhone());
        logininfo.setSalt(salt);
        logininfo.setPassword(md5Pwd);
        logininfo.setType(1);
        logininfo.setDisable(1);
        return logininfo;
    }

    private User loginInfoToUser(Logininfo logininfo){
        User user = new User();
        //bean拷贝，把相同字段的值拷贝到另一个对象的相同字段去
        BeanUtils.copyProperties(logininfo,user);
        user.setState(1);
        user.setCreateTime(new Date());
        user.setLogininfoId(logininfo.getId());
        return user;
    }
}
